package cn.mahjong.service.play;

import cn.hserver.core.ioc.annotation.Bean;
import cn.hutool.json.JSONUtil;
import cn.mahjong.bean.MahJongUserBean;
import cn.mahjong.bean.PlayMsg;
import cn.mahjong.service.MahJongService;
import cn.mahjong.service.PlayService;

import java.util.List;
import java.util.Random;


/**
 * 处理房间加入信息
 */
@Bean
public class RoomJumpService implements PlayService {

    /**
     * 随机选择一个
     *
     * @param list
     * @param <T>
     * @return
     */
    private <T> T selectRandomElement(List<T> list) {
        Random random = new Random();
        int randomIndex = random.nextInt(list.size());
        return list.get(randomIndex);
    }

    @Override
    public void handler(MahJongService mahJongService) {
        //首次发牌通知
        if (mahJongService.getUserSize() == 4 && mahJongService.getMahJongSize() == 55) {
            //通知开始打牌
            MahJongUserBean mahJongUserBean = mahJongService.getUsers().stream().filter(k -> k.getCurrentMahJong().size() == 14).findFirst().get();
            //广播出去，现在开始出牌
            for (MahJongUserBean user : mahJongService.getUsers()) {
                user.getWs().send(JSONUtil.toJsonStr(new PlayMsg(user.getUsername(), PlayMsg.Type.TIPS, "等待玩家：" + mahJongUserBean.getUsername() + "出牌")));
            }
        }
    }
}
